package Server;
import client.*;
// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 24/05/2011 14:48:34
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Server.java

import gui.MainPanel;

import java.io.IOException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.sql.*;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JFrame;

import ocsf.server.AbstractServer;
import ocsf.server.ConnectionToClient;
import client.Item;

public class Server extends AbstractServer
{
	public static MainScreen aFrame = new MainScreen();
	public static final int DEFAULT_PORT = 5555;
	public catalogServ cat = new catalogServ();
	public exit off = new exit();
    public Server(int port)
    {
        super(port);
    }

    public void handleMessageFromClient
    (Object msg, ConnectionToClient client)
  {   
    	InetAddress temp = client.getInetAddress();
    	
    	aFrame.jTextFieldNumc.setText(client.toString());
    	aFrame.jTextFieldNum.setText(Integer.toString(client.activeCount()));
    	
    	Object resMsg=null;
  	try
      {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
      }
      catch(Exception exception) { }
      try
      {
      	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/Flower shop", "root", "Braude");
          System.out.println("SQL connection succeed");
        
          
          if (msg instanceof String)
          {  
          String input = (String)msg;
          if (input.equalsIgnoreCase("num_of_items"))
     	 {
     	 
     		 	 
   		   int num_of_items=Order.ReturnNumOfItems((com.mysql.jdbc.Connection) conn);
   		   try {
					client.sendToClient(num_of_items);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}       
				}
          }
          
          
          
          
          
          
          
          
          
          
          
          
          
          if (msg instanceof Vector)
          {          	  
        	  Vector<Object> MOrder = (Vector)msg;;          
          if (MOrder.firstElement().equals("order"))
          {                    	
   		   Order.MainOrder((com.mysql.jdbc.Connection) conn, MOrder);
          }
				
          }
          
          
          
          
          
          
          
          
          
          
          
          
          
                   
          
         if (msg instanceof String[])
         {  
        	 String[] input = (String[])msg;
        	 
        	 if (input[0].equalsIgnoreCase("login"))
        	 {
        	 
        		 String[] logstate =new String[6]; 	  
      		   logstate=login.chackvalidation((com.mysql.jdbc.Connection) conn, input);
      		   try {
					client.sendToClient(logstate);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}       
				} 	
        	 if (input[0].equalsIgnoreCase("logoff"))
        	 {        	         					  
      		   logoff.chackusertologoff((com.mysql.jdbc.Connection) conn, input);
     
				}
        	 if (input[0].equalsIgnoreCase("OrderSearch"))
        	 {
        		 ArrayList a = new ArrayList<String[]>();
        		 a=Order.SearchItemOredr((com.mysql.jdbc.Connection) conn, input);
        		 
        		 try {
 					client.sendToClient(a);
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				} 
        	 }
 				if (input[0].equalsIgnoreCase("check-subscription"))
 	        	 { 	        		 
 					Vector<Object> subdeatails = new Vector<Object>();
 					subdeatails=Order.checksub((com.mysql.jdbc.Connection) conn, input);
 	        		 
 	        		 try {
 	 					client.sendToClient(subdeatails);
 	 				} catch (IOException e) {
 	 					// TODO Auto-generated catch block
 	 					e.printStackTrace();
 	 				}		
        	 }
 				if (input[0].equalsIgnoreCase("updatebalance"))
	        	 { 	        		 
					
					Order.updatebalance((com.mysql.jdbc.Connection) conn, input);
	        		 	        				
       	 }
 				if (input[0].equalsIgnoreCase("new_credit-card"))
	        	 { 	        		 
					
					Order.enternewcreditcard((com.mysql.jdbc.Connection) conn, input);
	        		 	        				
	        	 }	
 				if (input[0].equalsIgnoreCase("last_time"))
	        	 { 	        		 
					Vector<Object> carddeatails = new Vector<Object>();
					carddeatails=Order.creditcard((com.mysql.jdbc.Connection) conn, input);
	        		 
	        		 try {
	 					client.sendToClient(carddeatails);
	 				} catch (IOException e) {
	 					// TODO Auto-generated catch block
	 					e.printStackTrace();
	 				}		
	        	 }
 				
 				if (input[0].equalsIgnoreCase("check_inventory"))
	        	 { 	        		
 					String[] answer=new String[2];
					answer=Order.Inventory((com.mysql.jdbc.Connection) conn, input);
	        		 
	        		 try {
	 					client.sendToClient(answer);
	 				} catch (IOException e) {
	 					// TODO Auto-generated catch block
	 					e.printStackTrace();
	 				}		
	        	 }
 				
 				if (input[0].equalsIgnoreCase("get_pic"))
	        	 { 	        		
 					resMsg = Order.GetPic((com.mysql.jdbc.Connection) conn,input);						        		 
	        		 try {
	 					client.sendToClient(resMsg);
	 				} catch (IOException e) {
	 					// TODO Auto-generated catch block
	 					e.printStackTrace();
	 				}		
	        	 }
 				
 				
 				
        	 if (input[0].equalsIgnoreCase("block"))
        		{
        		 String[] block =new String[2]; 	
      		     block=BlockingUser.blockhim((com.mysql.jdbc.Connection) conn, input);
      		   try {
					client.sendToClient(block);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}       
				} 	
	 
        	 if (input[0].equalsIgnoreCase("userdet"))
     		{
     		 String[] userid =new String[4]; 	
   		     userid=Getuserid.getuserdet((com.mysql.jdbc.Connection) conn, input);
   		   try {
					client.sendToClient(userid);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}       
				} 	
        	 if (input[0].equalsIgnoreCase("complaint"))
     		{
        		 String[] comp =new String[5]; 	
       		     comp=Compinsert.insertcomp((com.mysql.jdbc.Connection) conn, input);
       		   try {
    					client.sendToClient(comp);
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}       
    				} 
        	 if (input[0].equalsIgnoreCase("chacknamevalid"))
      		{
         		 String[] comp =new String[5]; 	
        		     comp=Compinsert.chackname((com.mysql.jdbc.Connection) conn, input);
        		   try {
     					client.sendToClient(comp);
     				} catch (IOException e) {
     					// TODO Auto-generated catch block
     					e.printStackTrace();
     				}       
     				} 
     	 
        	 if (input[0].equalsIgnoreCase("get_orders"))
        	 {
        		 Vector a = new Vector<Object>();
        		 a=CancelOrder.SearchOredrs((com.mysql.jdbc.Connection) conn, input);
        		 
        		 try {
 					client.sendToClient(a);
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				} 
        	 }
        	 if (input[0].equalsIgnoreCase("block"))
     		{
     		 String[] block =new String[2]; 	
   		     block=BlockingUser.blockhim((com.mysql.jdbc.Connection) conn, input);
   		   try {
					client.sendToClient(block);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}       
				} 	
	 
     	 if (input[0].equalsIgnoreCase("userdet"))
  		{
  		 String[] userid =new String[4]; 	
		     userid=Getuserid.getuserdet((com.mysql.jdbc.Connection) conn, input);
		   try {
					client.sendToClient(userid);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}       
				} 	
     
  	 
     	 if (input[0].equalsIgnoreCase("get_orders"))
     	 {
     		 Vector a = new Vector<Object>();
     		 a=CancelOrder.SearchOredrs((com.mysql.jdbc.Connection) conn, input);
     		 
     		 try {
					client.sendToClient(a);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
     	 }

     	 if (input[0].equalsIgnoreCase("getcompid"))
     	 {
     		 String[] a = new String[2];
     		 a=Searccomemploy.getid((com.mysql.jdbc.Connection) conn);
     		 
     		 
     		 try {
					client.sendToClient(a);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
     	 }
           if (input[0].equalsIgnoreCase("serchcompbycustomer"))
             {
	              ArrayList a = new ArrayList<String[]>();
	              a=Searccomemploy.Searchccustomer((com.mysql.jdbc.Connection) conn, input);
	 
	 
	                try {
	                	client.sendToClient(a);
	                    } catch (IOException e) {
	                	// TODO Auto-generated catch block
	                  	e.printStackTrace();
	            } 
           }
           if (input[0].equalsIgnoreCase("serchcompbyemploy"))
      	 {
      		 ArrayList a = new ArrayList<String[]>();
      		 a=Searccomemploy.Searchcomemploy((com.mysql.jdbc.Connection) conn, input);
      		 
      		 
      		 try {
					client.sendToClient(a);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
      	 }
           if (input[0].equalsIgnoreCase("closecomplainadngivmon"))
        	 {
        	   String[] data = new String[3];
        	   Searccomemploy.moneyandclose((com.mysql.jdbc.Connection) conn, input);
        		 
        		 
        		 try {
  					client.sendToClient(data);
  				} catch (IOException e) {
  					// TODO Auto-generated catch block
  					e.printStackTrace();
  				} //tranmoney
        	 }
             if (input[0].equalsIgnoreCase("closecomplain"))
      	 {
      	   String[] data = new String[3];
      	   Searccomemploy.closecomp((com.mysql.jdbc.Connection) conn, input);
      		 
      		 
      		 try {
					client.sendToClient(data);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} //tranmoney
      	 }
           if (input[0].equalsIgnoreCase("givemoney"))
      	 {
      	   String[] data = new String[3];
      	   Searccomemploy.money((com.mysql.jdbc.Connection) conn, input);
      		 
      		 
      		 try {
					client.sendToClient(data);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
      	 }
      
        	 if (input[0].equalsIgnoreCase("cancel_order"))
        	 { 	        		 
				String[] data = new String[3];
				data=CancelOrder.CancelOredrs((com.mysql.jdbc.Connection) conn, input);
				try {
 					client.sendToClient(data);
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}        				
        	 }
        	 
        	 if (input[0].equalsIgnoreCase("check_balance"))
        	 { 	        		 
				int a;
				a=CancelOrder.checkbalance((com.mysql.jdbc.Connection) conn, input);
				try {
 					client.sendToClient(a);
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}        				
        	 }
        	 

        	
         	
         	if (input[0].equalsIgnoreCase("report"))
       	 {
       		 
       		 Object[][] stri;
       		 System.out.println("aaaa");
       		 
       		 stri = MyReport.func((com.mysql.jdbc.Connection) conn, input);
       		 
       		   try {
  					client.sendToClient(stri);
  				} catch (IOException e) {
  					// TODO Auto-generated catch block
  					e.printStackTrace();
  				}       
  					
       		 
       		 
       		 
       		 
       	 }
       	 
        	 if (input[0].equalsIgnoreCase("questionnare"))
       	 {
       		 MyQuestionnare.func((com.mysql.jdbc.Connection) conn, input);
       		 
       	 }
        	 
        	 
        	if (input[0].equalsIgnoreCase("questionnare_start"))
      	 {
        		int[] inRet;        		
        		inRet = MyQuestionnareStart.func((com.mysql.jdbc.Connection) conn, input);
        		try {
					client.sendToClient(inRet);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
      		 
      	 }
     	
         	
        }
         
         
         if (msg instanceof catalog){
        	 if (((catalog)msg).getAction()==1){
        		resMsg = cat.getAllCategory(conn);
        	 }
             else if (((catalog)msg).getAction()==2){
        		resMsg = cat.getAllType(conn);
        	 }
             else if (((catalog)msg).getAction()==3){
            	 System.out.println("server check");
            	 resMsg = cat.getItemToCatalog(conn,msg);
             }	
             else if (((catalog)msg).getAction()==4){
            	 resMsg = cat.catalogDisply(conn);
             }
             else if (((catalog)msg).getAction()==5){
            	 System.out.println("check");
            	 resMsg = cat.ItemDelete(conn,msg);
             }
             else if (((catalog)msg).getAction()==6){
            	 System.out.println("ch");
            	 resMsg = cat.SaleUpdate(conn,msg);
             }
             else if (((catalog)msg).getAction()==7){
            	 System.out.println("ch");
            	 resMsg = cat.SaleAdd(conn,msg);
             }
             else if (((catalog)msg).getAction()==8){
            	 System.out.println("cssss");
            	 resMsg = cat.SaleDelete(conn,msg);
             }
        	 
        	 try {
					client.sendToClient(resMsg);
					
        	 } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
        	 	}
         }
         else if (msg instanceof quit){
        	 off.windowClose(conn, msg);
         }
      }
      
      catch(SQLException ex)
      {
          System.out.println((new StringBuilder("SQLException: ")).append(ex.getMessage()).toString());
          System.out.println((new StringBuilder("SQLState: ")).append(ex.getSQLState()).toString());
          System.out.println((new StringBuilder("VendorError: ")).append(ex.getErrorCode()).toString());
      }
  }
  

    protected void serverStarted()
    {        
        aFrame.jTextFieldMessage.setText("Server listening for connections on port "+ (Integer.toString(getPort())));
    }

    protected void serverStopped()
    {        
        aFrame.jTextFieldMessage.setText("Server has stopped listening for connections.");
    }

    public static void main(String args[])
    {
        int port = 0;
        try
        {
            port = Integer.parseInt(args[0]);
        }
        catch(Throwable t)
        {
            port = DEFAULT_PORT;
        }
        Server sv = new Server(port);
        try
        {
            sv.listen();
        }
        catch(Exception ex)
        {
        	aFrame.jTextFieldMessage.setText("ERROR - Could not listen for clients!");
        }
        aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );                
        aFrame.setSize( 600,600 ); // set frame size
        aFrame.setResizable(true);
        aFrame.setVisible( true ); // display frame
        
    }

    
}
